// 商品模型
const { Goods } = require('../../../models/good');
// 统一数据返回类
const { DataReturn } = require('../../../common/returnclass');

module.exports = async (req, res) => {
	// 三级分类id type: 0综合排序 1 销量优先 2价格排序 sort:1 升序 2 降序
	let { pagenum, _id, type, sort } = req.query;
	console.log(req.query);
	// 查询条数
	let pagesize = 10;
	
	if (type == 0) {
		// 根据添加时间降序
		goods = await Goods.find({goods_cate:_id}).select('-goods_content').sort({add_time: -1}).skip(pagesize*(pagenum-1)).limit(pagesize);
	} else if (type == 1) {
		// 根据销量降序
		goods = await Goods.find({goods_cate:_id}).select('-goods_content').sort({sales_volume: -1}).skip(pagesize*(pagenum-1)).limit(pagesize);
	} else if (type == 2) {
		if (sort == 1) {
			// 根据价格升序
			goods = await Goods.find({goods_cate:_id}).select('-goods_content').sort({goods_price: 1}).skip(pagesize*(pagenum-1)).limit(pagesize);
		} else {
			// 根据价格升序
			goods = await Goods.find({goods_cate:_id}).select('-goods_content').sort({goods_price: -1}).skip(pagesize*(pagenum-1)).limit(pagesize);
		}
	}
	
	if (!goods) return DataReturn.getData(res, 200, goods, '暂无商品数据');
	// 响应
	DataReturn.getData(res, 200, { goods:goods, total: await Goods.find({goods_cate:_id}).countDocuments() }, '查询商品列表成功')

}
